<template>
  <van-nav-bar right-text="注册" @click-right="getTop"/>
  <van-form ref="form" v-model="form">
    <h3>用户登录</h3>
    <van-cell-group inset>
      <van-field
          v-model="form.userName"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="rules.userName"
      />
      <van-field
          v-model="form.password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="rules.password"
      />
    </van-cell-group>
    <div style="margin: 16px">
      <van-button
          round
          block
          type="primary"
          native-type="submit"
          @click="onSubmit(form)"
      >
        登录
      </van-button>
    </div>
  </van-form>
  <p>
    没有账号?
    <router-link to="/register">注册一个</router-link>
  </p>
</template>

<script>
import {login, lookCurrentUser} from "../api/user";
import jwtDecode from "jwt-decode";
import {showToast} from "vant";

export default {
  data() {
    return {
      currentUser: {},
      isAdmin: false,
      form: {
        userName: "user1",
        password: "12345678",
      },
      //随机数仓库
      identifyCodes: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
      //验证码
      identifyCode: "",
      rules: {
        userName: [{required: true, message: "用户名不得为空!"}],
        password: [
          {required: true, message: "密码不得为空!"},
          {
            required: true,
            validator: (val) => {
              if (val.length < 6 || val.length > 12) {
                return "密码长度介于6~12位之间";
              }
            },
          },
        ],
      },
    };
  },
  created() {
  },
  methods: {
    onSubmit(user) {
      this.$refs.form
          .validate()
          .then(() => {
            login(user.userName, user.password).then((req) => {
              if (req.data.success) {
                //获取token
                var token = req.data.data;
                var user = jwtDecode(token).user;
                sessionStorage.setItem("token", token);
                this.lookCurrentUser(user);
                setTimeout(() => {
                  console.log(sessionStorage.getItem("per"));
                  if (sessionStorage.getItem("per")[2] === "N") {
                    showToast(`对不起,你无权访问此系统!`);
                    return;
                  }
                  showToast(`登录成功`);
                  sessionStorage.setItem("user", user);
                  this.$router.push({name: "page"});
                }, 500);
              } else {
                showToast(`${req.data.message}`);
              }
            });
          })
          .catch(() => {
            return false;
          });
    },
    //获取当前用户
    lookCurrentUser(uid) {
      lookCurrentUser(uid).then((req) => {
        this.currentUser = req.data.data;
        sessionStorage.setItem("per", this.currentUser.permissions);
      });
    },
    //返回上一级
    getTop() {
      this.$router.push({name: "register"});
      console.log("返回");
    },
  },
};
</script>

<style lang="less" scoped>
h3 {
  text-align: center;
}

p {
  text-align: center;
}
</style>